package org.mclll.xwangplugin.town.lp;

import com.github.promeg.pinyinhelper.Pinyin;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.ConsoleCommandSender;
import org.mclll.xwangplugin.town.enums.TownParty;

import java.util.List;
import java.util.Locale;

public class LuckPermsTown {
    private static Server server = Bukkit.getServer();
    private static ConsoleCommandSender consoleSender = Bukkit.getConsoleSender();

    public static void creteTown(String townName, TownParty party) {
        String create = "lp creategroup %s";
        String displayName = "lp group %s setdisplayname %s";
        String setParent = "lp group %s parent set %s";
        String partName = party == TownParty.DAWN ? "poxiao" : "penglai";
        String setPrefix = "lp group %s meta addprefix 1 [%s]";
        String townGroupName = Pinyin.toPinyin(townName, "").toLowerCase(Locale.ROOT);
        server.dispatchCommand(consoleSender, String.format(create, townGroupName));
        server.dispatchCommand(consoleSender, String.format(displayName, townGroupName, townName));
        server.dispatchCommand(consoleSender, String.format(setParent, townGroupName, partName));
        server.dispatchCommand(consoleSender, String.format(setPrefix, townGroupName, townName));
    }

    public static void deleteTown(String townName) {
        String command = "lp deletegroup %s";
        String townGroupName = Pinyin.toPinyin(townName, "").toLowerCase(Locale.ROOT);
        server.dispatchCommand(consoleSender,
                String.format(command, townGroupName)
        );
    }

    public static void addPlayerToTown(String townName, List<String> playerNameList) {
        String command = "lp user %s parent set %s";
        String townGroupName = Pinyin.toPinyin(townName, "").toLowerCase(Locale.ROOT);
        for (String name : playerNameList) {
            server.dispatchCommand(consoleSender,
                    String.format(command, name, townGroupName)
            );
        }
    }

    public static void kickPlayerFromTown(List<String> playerNameList) {
        String command = "lp user %s parent set nonparty";
        for (String name : playerNameList) {
            server.dispatchCommand(consoleSender,
                    String.format(command, name));
        }

    }

    public static void addLeader(String name) {
        String command = "lp user %s permission set mclll.nameplate.镇长 true";
        server.dispatchCommand(consoleSender,
                String.format(command, name));
    }

    public static void delLeader(String name) {
        String command = "lp user %s permission unset mclll.nameplate.镇长";
        server.dispatchCommand(consoleSender,
                String.format(command, name));
    }

}
